/**
 * Copyright (C) 2018-2022
 * All rights reserved, Designed By www.yixiang.co
 * 注意：
 * 本软件为www.yixiang.co开发研制
 */
package co.yixiang.controller.buyer.shop;

import cn.hutool.core.util.ObjectUtil;
import co.yixiang.common.annotation.Log;
import co.yixiang.common.enums.SettingEnum;
import co.yixiang.common.utils.ResultUtil;
import co.yixiang.common.vo.ResultMessage;
import co.yixiang.modules.config.entity.domain.Setting;
import co.yixiang.modules.config.entity.dto.setting.MenusBooleanSetting;
import co.yixiang.modules.config.service.SettingService;
import co.yixiang.modules.shop.entity.dto.YxSystemGroupDataDto;
import co.yixiang.modules.shop.entity.dto.YxSystemGroupDataQueryCriteria;
import co.yixiang.modules.shop.service.YxSystemGroupDataService;
import com.google.gson.Gson;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author hupeng
 * @date 2019-10-18
 */
@Api(tags = "商城:首页")
@RestController
@RequestMapping("/home")
public class HomeBuyerController {

    @Autowired
    private YxSystemGroupDataService yxSystemGroupDataService;

    /**
     * 设置
     */
    @Autowired
    private SettingService settingService;

    @Log("查询Banner图")
    @ApiOperation(value = "查询Banner图")
    @GetMapping(value = "/banner")
    public ResultMessage<List<YxSystemGroupDataDto>> getBanners(YxSystemGroupDataQueryCriteria criteria, Pageable pageable) {
        criteria.setGroupName("yshop_home_banner");
        final List<YxSystemGroupDataDto> yxSystemGroupDataDtos = yxSystemGroupDataService.queryAllByVo(criteria);
        return ResultUtil.data( yxSystemGroupDataDtos);
    }

    @Log("查询首页滚动新闻")
    @ApiOperation(value = "查询首页滚动新闻")
    @GetMapping(value = "/rollNews")
    public ResultMessage<List<YxSystemGroupDataDto>> getRollNews(YxSystemGroupDataQueryCriteria criteria, Pageable pageable) {
        criteria.setGroupName("yshop_home_roll_news");
        final List<YxSystemGroupDataDto> yxSystemGroupDataDtos = yxSystemGroupDataService.queryAllByVo(criteria);
        return ResultUtil.data( yxSystemGroupDataDtos);
    }


    @Log("查询首页导航按钮")
    @ApiOperation(value = "查询首页导航按钮")
    @GetMapping(value = "/menus")
    public ResultMessage<List<YxSystemGroupDataDto>> getMenus(YxSystemGroupDataQueryCriteria criteria, Pageable pageable) {
        criteria.setGroupName("yshop_home_menus");
//        Sort sort = Sort.by(Sort.Direction.DESC, "sort");
//        Pageable pageableT = PageRequest.of(pageable.getPageNumber(), pageable.getPageSize(), sort);
//        return ResultUtil.data(yxSystemGroupDataService.queryAll(criteria, pageableT));
        final List<YxSystemGroupDataDto> yxSystemGroupDataDtos = yxSystemGroupDataService.queryAllByVo(criteria);
        return ResultUtil.data( yxSystemGroupDataDtos);
    }


    @Log("查询首页菜单栏展示")
    @ApiOperation(value = "查询首页菜单栏展示")
    @GetMapping(value = "/menus/boolean")
    public ResultMessage<String> menusBool() {
        Setting setting = settingService.get(SettingEnum.MENUS_BOOLEAN.name());
        if (ObjectUtil.isNotEmpty(setting)) {
            //如果不需要审核则审核自动通过
            MenusBooleanSetting withdrawalSetting = new Gson().fromJson(setting.getSettingValue(), MenusBooleanSetting.class);
            if (Boolean.TRUE.equals(withdrawalSetting.getApply())) {
                return ResultUtil.data("1");
            }
        }
        return ResultUtil.data("0");
    }





}
